package com.xu.sms.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.xu.sms.dao.InfoDao;
import com.xu.sms.entity.Info;
import com.xu.sms.service.InfoService;
import com.xu.sms.utils.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xupenghao@163.com
 * @date 2018-06-03 19:59
 */

@Service
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoDao infoDao;

    @Override
    @Transactional
    public void add(Info info) {
        infoDao.add(info);
    }

    @Override
    public List<Info> finAll() {
        return null;
    }

    @Transactional
    @Override
    public void delete(int infoId) {
        infoDao.delete(infoId);
    }

    @Override
    public void update(Info info) {
        infoDao.update(info);
    }

    @Override
    public Info findInfoById(int infoId) {
        return infoDao.findInfoById(infoId);
    }

    @Override
    public List<Info> findAllByPaging(Integer pCur,String keyword,Integer typeId) {
        Map<String,Object> params = new HashMap<String,Object>();
        PageResult pr = new PageResult();
        pr.setpStart(pCur);
        params.put("pStart",pr.getpStart());//设置数据库中的起始页
        params.put("pSize",PageResult.PAGERESULT_PSIZE);
        setInfoMapParams(keyword, typeId, params);
        return infoDao.findAllByPaging(params);
    }

    private void setInfoMapParams(String keyword, Integer typeId, Map<String, Object> params) {
        if(!StringUtils.isEmpty(keyword)){
            keyword = "%"+keyword+"%";
        }else{
            keyword =null;
        }
        if(typeId ==null || typeId==0){
            typeId =null;
        }
        params.put("keyword",keyword);
        params.put("typeId",typeId);
    }

    @Override
    public List<String> findInfoByTypeId(int typeId) {
        return infoDao.findInfoByTypeId(typeId);
    }

    @Override
    public List<String> findInfoBykeyWord(String keyword) {
        keyword = "%"+keyword+"%";
        return  infoDao.findInfoBykeyWord(keyword);
    }

    @Override
    public Map<String, Object> findInfoAndTypeById(Integer infoId) {

        return infoDao.findInfoAndTypeById(infoId);
    }

    @Override
    public int getTotalCount(String keyword,Integer typeId){
        Map<String,Object> params = new HashMap<String,Object>();
        setInfoMapParams(keyword, typeId, params);
        return infoDao.getTotalCount(params);
    }
}
